package com.app.service.impl;

import com.app.mapper.AwardInfoMapper;
import com.app.model.AwardInfoModel;
import com.app.service.AwardInfoService;
import com.app.utils.DateMaskingUtils;
import com.app.vo.AwardInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * 获奖信息
 *
 * @author 16693103420
 * @since 2024-01-25
 */
@Service
public class AwardInfoServiceImpl extends ServiceImpl<AwardInfoMapper, AwardInfoModel> implements AwardInfoService {

    @Autowired
    private AwardInfoMapper awardInfoMapper;

    @Override
    public AwardInfoVo getAwardInfoList(Integer page, Integer limit, Integer activityId) {
        Page<AwardInfoModel> page1 = new Page<>(page, limit);
        QueryWrapper<AwardInfoModel> qw = new QueryWrapper<>();
        if (activityId != null) {
            qw.eq("activity_id", activityId);
        }
        qw.orderByDesc("create_time");
        Page<AwardInfoModel> awardInfoPage = awardInfoMapper.selectPage(page1, qw);
        List<AwardInfoModel> awardInfoList = awardInfoPage.getRecords();
        for (AwardInfoModel awardInfo : awardInfoList) {
            //计算过去时间
            Timestamp createTime = awardInfo.getCreateTime();
            Timestamp nowTime = new Timestamp(System.currentTimeMillis());
            long time = nowTime.getTime() - createTime.getTime();
            long lastTimeMinute = time / 1000 / 60;
            String pastTime = null;
            if (lastTimeMinute < 60) {
                String minute = String.valueOf(lastTimeMinute);
                pastTime = minute + "分钟前";
            } else if (lastTimeMinute > 60 && lastTimeMinute < 60 * 24) {
                String hour = String.valueOf(lastTimeMinute / 60);
                pastTime = hour + "小时前";
            } else if (lastTimeMinute > 60 * 24 && lastTimeMinute < 60 * 24 * 7) {
                String day = String.valueOf(lastTimeMinute / 60 / 24);
                pastTime = day + "天前";
            } else {
                pastTime = "1周前";
            }
            awardInfo.setPastTime(pastTime);
            //对手机号进行脱敏处理
            String phoneMi = DateMaskingUtils.mobilePhone(awardInfo.getPhone());
            awardInfo.setPhoneMi(phoneMi);
        }

        long count = awardInfoPage.getTotal();
        AwardInfoVo awardInfoVo = new AwardInfoVo();
        awardInfoVo.setList(awardInfoList);
        awardInfoVo.setCount((int) count);
        return awardInfoVo;
    }
}
